package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcNursingGroupNewMapper;
import com.csun.cmny.provider.model.domain.OmcNursingGroupNew;
import com.csun.cmny.provider.model.dto.NursingRoomDto;
import com.csun.cmny.provider.model.dto.OmcNursingGroupNewDto;
import com.csun.cmny.provider.model.vo.OmcUserVo;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.service.OmcGroupService;
import com.csun.cmny.provider.service.OmcNursingGroupService;
import com.csun.cmny.provider.service.UacRpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OmcNursingGroupServiceImpl extends BaseService<OmcNursingGroupNew> implements OmcNursingGroupService {

    @Autowired
    private OmcNursingGroupNewMapper nursingGroupNewMapper;
    @Autowired
    private UacRpcService uacRpcService;
    @Autowired
    private OmcGroupService omcGroupServiceImpl;

    /**
     * 存储班组人员，房间信息
     */
    @Override
    public void saveOther(OmcNursingGroupNewDto nursingGroupNewDto) {
        nursingGroupNewMapper.saveOtherUser(nursingGroupNewDto);
        nursingGroupNewMapper.saveOtherRoom(nursingGroupNewDto);
    }
    /*
    * 查询班组
    * */
    @Override
    public List<OmcNursingGroupNewDto> selectNursingGroup(OmcNursingGroupNewDto nursingGroupNewDto) {
        return nursingGroupNewMapper.selectNursingGroup(nursingGroupNewDto);
    }
    /**
     * 删除班组人员，房间信息
     */
    @Override
    public void deleteOther(Long id) {
        nursingGroupNewMapper.deleteOtherUser(id);
        nursingGroupNewMapper.deleteOtherRoom(id);
    }

    @Override
    public List<Room> selectNursingBindRoom(Long nursingGroupId) {
        return nursingGroupNewMapper.selectNursingBindRoom(nursingGroupId);
    }
    /**
     * 通过buildingId查询该机构没有被分配的房间
     */
    @Override
    public List<Room> selectByBuildingId(Long buildingId) {
        return nursingGroupNewMapper.selectByBuildingId(buildingId);
    }
    /**
     * 查询护工组详情
     */
    @Override
    public List<OmcUserVo> selectNuringUserInfo(Long id) {
        List<OmcUserVo> omcUserVos = omcGroupServiceImpl.selectByGroupId(id);
        //获取护工id
        List<Long> userIds = new ArrayList<>();
        for (OmcUserVo o:omcUserVos) {
            userIds.add(o.getUserId());
        }
        if(userIds.size() > 0){
            List<NursingRoomDto> nursingRoomDtos = nursingGroupNewMapper.selectByGroupId(userIds);
            for (OmcUserVo o:omcUserVos) {
                for (NursingRoomDto n:nursingRoomDtos) {
                    if(o.getUserId().equals(n.getUserId())){
                        o.setRoomNos(n.getRoomNos());
                    }
                }
            }
        }
        return omcUserVos;
    }
}
